/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <io.h>
#include <conio.h>
#include <direct.h>

#include "debughelper/debughelper.h"
#include "properties/properties.h"
#include "kmlinterface.h"
#include "kmlopenjauscomponent.h"


const char *KMLJGP_VERSION = "V1.0";

void usage() {
	printf("Usage: KMLJGP [config_filename]\n");
	exit(0);
}

int main(int argc, char *argv[]) {
#ifdef _DEBUG
	// turn on memoery leak detection
	// in a watch window, set the global variable
	// {,,msvcr80d.dll}_crtBreakAlloc 
	// to break on offending memory block allocation
	int tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(tmpDbgFlag);
#endif

	std::string prop_filename = "kmljgpConfig.txt";

	printf("KML JAUS Global Pose sensor %s\n", KMLJGP_VERSION);

	// check command line arguments
	if (argc >= 3) {
		usage();
	} else if (argc >= 2) {
		if ((argv[1][0] == '?') || (argv[1][1] == '?')) {
			usage();
		}
		prop_filename = argv[1];
	}

	// load properties file
	prop::Properties properties;
	bool prop_flag = properties.load(prop_filename);
	std::string kml_filename    = properties.getProperty     ("kml_filename",         "kmljgp.kml");
	double playbackRate_hz      = properties.getFloatProperty("playbackRate_hz",      10.0);
	bool   loopCoordinates_flag = properties.getBoolProperty ("loopCoordinates_flag", true);
	if (prop_flag) {
		printf("Loaded properties file \"%s\"\n", prop_filename.c_str());
		properties.listUnrecognized(stderr, "Unrecognized properties:\n");
	} else {
		fprintf(stderr, "Failed to open properties file \"%s\"\n", prop_filename.c_str());
		if (properties.list(prop_filename, "# KML JAUS Global Pose sensor config file\n")) {
			fprintf(stderr, "Created properties file \"%s\" with default values\n", prop_filename.c_str());
		}
	}

	// create kml interface
    kmljgp::KmlInterface *kmlInterface = new kmljgp::KmlInterface();
	if (kmlInterface->init(kml_filename, loopCoordinates_flag)) {
		kmlInterface->start(playbackRate_hz);

		// create kml open jaus component
        kmljgp::KmlOpenJausComponent *kmlComponent =
            new kmljgp::KmlOpenJausComponent(kmlInterface);
		if (kmlComponent->init(10.0)) {
			kmlComponent->start();
			
			// wait for user to stop
			int c = 0;
			do {
				c = _getch();
			} while (c != 0x1B);

			// stop kml open jaus component
			kmlComponent->stop();
		}
		delete kmlComponent;
		kmlComponent = NULL;

		// stop kml interface
		kmlInterface->stop();
	}
	delete kmlInterface;
	kmlInterface = NULL;

	// exit app
	return 0;
}
